package com.avic.rong.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.api.login_register.BindPhoneApi;
import com.avic.rong.api.SendCodeApi;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.RegexUtils;
import com.avic.rong.utils.StringUtils;
import com.avic.rong.utils.TimeCount;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

//账号安全-绑定手机号
public class BindPhoneActivity extends BaseActivity {

    @BindView(R.id.edittext_phone)
    EditText edittext_phone;
    @BindView(R.id.edittext_code)
    EditText edittext_code;
    @BindView(R.id.bt_send_code)
    Button bt_send_code;
    @BindView(R.id.btn_ok)
    Button btn_ok;
    private boolean countDownTimeEnd = true;
    private SendCodeApi sendCodeApi;
    private Disposable countDownDisposable;
    private BindPhoneApi bindPhoneApi;
    private String phone;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_bind_phone;
    }

    @Override
    protected void initView() {
        tvTitle.setText("绑定手机");
    }

    @Override
    protected void addListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public static Intent queryIntent(Context context) {
        Intent intent = new Intent(context, BindPhoneActivity.class);
        return intent;
    }

    @OnClick({R.id.bt_send_code, R.id.btn_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_send_code:
                sendCode();
                break;
            case R.id.btn_ok:
                bindPhone();
                break;
        }
    }

    private void bindPhone() {
        String code = edittext_code.getText().toString().trim();
        phone = edittext_phone.getText().toString().trim();
        if (StringUtils.isEmpty(phone)|| StringUtils.isEmpty(code)) {
            Toast toast = Toast.makeText(this, "手机号码或验证码不能为空", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return;
        }
        if (!RegexUtils.isMobileExact(phone)) {
            Toast toast = Toast.makeText(this, "手机号码格式不正确", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return;
        }
        String login_token = SharedPreferencesUtil.getLoginToken();
        bindPhoneApi = new BindPhoneApi(this,bindPhoneListener);

        bindPhoneApi.setArguments(phone,login_token,code);

        HttpManager.getInstance().doHttpDeal(bindPhoneApi);


    }

    private void sendCode() {
        if (!countDownTimeEnd) return;
        sendCodeApi = new SendCodeApi(this, sendCodeListener);

        String phone = edittext_phone.getText().toString().trim();
        if (StringUtils.isEmpty(phone)) {
            Toast toast = Toast.makeText(this, "手机号码不能为空", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return;
        }
        if (!RegexUtils.isMobileExact(phone)) {
            Toast toast = Toast.makeText(this, "手机号码格式不正确", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return;
        }
        sendCodeApi.setArguments(phone, 8, SharedPreferencesUtil.SIGN);
        HttpManager.getInstance().doHttpDeal(sendCodeApi);
    }

    HttpOnNextListener<BaseResultEntity> sendCodeListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
                timeOut();
            }else {
                Toast toast = Toast.makeText(BindPhoneActivity.this, baseResultEntity.getMsg(), Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
    };

    HttpOnNextListener<BaseResultEntity> bindPhoneListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
                ToastTools.showToast("绑定手机号成功！");
            }else {
                ToastTools.showToast(baseResultEntity.getMsg());
            }
        }
    };

    private void timeOut() {
        TimeCount timeCount = new TimeCount(60000, 1000, bt_send_code,true);
        timeCount.start();
    }
}
